<?php

namespace app\admin\controller;

use app\admin\model\NewsModel;
use app\admin\validate\NewsValidate;
use think\Controller;
use think\db\Query;
use think\Request;

class News extends Controller
{
    //编辑新闻
    public function edit(){
         //接收要修改的数据id
         $id=input('id');
         //根据id获取到当前要修改的数据
        $row=NewsModel::get($id)->toArray();
        //查找新闻分类数据
        $cate = \app\admin\model\Cate::all()->toArray();
        return $this->fetch('news_edit', compact('cate','row'));
    }


    //添加新闻
    public function add(Request $request)
    {
        if ($request->isPost()) {
            //接收数据
            $data = $request->post();
            //验证
            $valid = new NewsValidate();
            if (!$valid->check($data)) {
                return $this->error($valid->getError());
            }
            //组装数据
            $data['add_time']=date('Y-m-d H:i:s');
            //入库
            $news_model=new NewsModel();
            $res=$news_model->save($data);
            if($res){
                return $this->success('添加成功','index');
            }else{
                return $this->error('添加失败');
            }
        }
        //查找新闻分类数据
        $cate = \app\admin\model\Cate::all()->toArray();
        //加载添加表单
        return $this->fetch('news_add', ['cate' => $cate]);
    }

    //新闻列表
    public function index()
    {
        //分类数据
        $cate = \app\admin\model\Cate::all()->toArray();
        //查询新闻数据
        return $this->fetch('news_list',compact('cate'));
    }

    //获取新闻数据
    public function newsData(){
        //分页数据
        $limit=input('get.limit'); //接收每页显示的条数
        //接收搜索条件：
        //分类id
        $cate_id=input('get.cate_id','');
        //起始时间
        $start=input('get.start','');
        //截止时间
        $end=input('get.end','');
        //tp中有个打印上条执行的SQL的方法：fetchSql()
       /* $data=NewsModel::with('cate')->fetchSql()->where(['data_flag'=>1])->where(function (Query $query) use ($cate_id,$start,$end){
            if($cate_id){
               $query->where('cate_id',$cate_id);
            }
            if($start){
                $query->where('add_time','>=',$start);
            }
            if($end){
                $query->where('add_time','<=',$end);
            }
        })->select();*/

        $data=NewsModel::with('cate')->where(['data_flag'=>1])->where(function (Query $query) use ($cate_id,$start,$end){
            if($cate_id){
                $query->where('cate_id',$cate_id);
            }
            if($start){
                $query->where('add_time','>=',$start);
            }
            if($end){
                $query->where('add_time','<=',$end);
            }
        })->paginate($limit)->toArray();


        /*
        if($cate_id==-1){
            if($start && $end==''){
                $data=NewsModel::with('cate')->where(['data_flag'=>1])->where('add_time','>=',$start)->paginate($limit)->toArray();
            }elseif ($start=='' && $end){
                $data=NewsModel::with('cate')->where(['data_flag'=>1])->where('add_time','<=',$end)->paginate($limit)->toArray();
            }elseif ($start && $end){
                $data=NewsModel::with('cate')->where(['data_flag'=>1])->where('add_time','>=',$start)->where('add_time','<=',$end)->paginate($limit)->toArray();
            }else{
                $data=NewsModel::with('cate')->where(['data_flag'=>1])->paginate($limit)->toArray();
            }
        }else{
            if($start && $end==''){
                $data=NewsModel::with('cate')->where(['data_flag'=>1,'cate_id'=>$cate_id])->where('add_time','>=',$start)->paginate($limit)->toArray();
            }elseif ($start=='' && $end){
                $data=NewsModel::with('cate')->where(['data_flag'=>1,'cate_id'=>$cate_id])->where('add_time','<=',$end)->paginate($limit)->toArray();
            }elseif ($start && $end){
                $data=NewsModel::with('cate')->where(['data_flag'=>1,'cate_id'=>$cate_id])->where('add_time','>=',$start)->where('add_time','<=',$end)->paginate($limit)->toArray();
            }else{
                $data=NewsModel::with('cate')->where(['data_flag'=>1,'cate_id'=>$cate_id])->paginate($limit)->toArray();
            }
        }
        */
        //返回layui表格要求的数据格式
        return json([
            'code'=>0,
            'msg'=>'成功',
            'data'=>$data['data'], //从data中拿到真正的数据，是一个二维数组
            'count'=>$data['total'] //获取总页数
        ]);
    }

    //新闻图片的上传
    public function upload()
    {
        //调用文件上传的函数
        return uploadOne();
    }

    //删除数据
    public function delNew(){
         //接收要删除的数据id
         $id=input('id');
         //修改数据状态
         $news_model=new NewsModel();
         $res=$news_model->save(['data_flag'=>-1],['id'=>$id]);
         if($res){
            return json([
                'code'=>200,
                'msg'=>'删除成功'
            ]);
         }else {
             return json([
                 'code'=>400,
                 'msg'=>'删除失败'
             ]);
         }
    }

}
